package com.cbh.domain.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbh.common.id.IdWorker;
import com.cbh.entity.Area;
import com.cbh.mapper.AreaMapper;

@Service
public class AreaService {
	private static final Log logger = LogFactory.getLog(AreaService.class);
	@Autowired
	AreaMapper areaMapper;

	/**
	 * 保存支付方式信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String save(Area ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		if (StringUtils.isEmpty(ob.getAreaId())) {
			return insert(ob);
		}
		return update(ob);
	}

	/**
	 * 保存支付方式信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String insert(Area ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		ob.setAreaId(IdWorker.getId().toString());

		if (null == ob.getAreaName()) {
			throw new Exception("请传入名称");
		}
		if (null == ob.getAreaCode()) {
			throw new Exception("请传入Code");
		}

		try {
			Integer re = areaMapper.insert(ob);
			if (re > 0) {
				return ob.getAreaId();
			} else {
				throw new Exception("保存数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("保存数据失败");
		}
	}

	/**
	 * 保存支付方式信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String update(Area ob) throws Exception {
		try {
			Integer re = areaMapper.update(ob);
			if (re > 0) {
				return ob.getAreaId();
			} else {
				throw new Exception("更新数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("更新数据失败");
		}
	}

	/**
	 * 真删除支付方式
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public boolean delete(String id) throws Exception {
		Area ob = new Area().id(id);
		try {
			Integer re = areaMapper.delect(ob);
			return re == 1;
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("删除数据失败");
		}
	}

	/**
	 * 根据proId获取支付方式信息
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public Area getOne(String id) throws Exception {
		if (null == id) {
			throw new Exception("主键为空");
		}
		Area ob = new Area().id(id);
		try {
			ob = areaMapper.queryOne(ob);
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return ob;
	}

	/**
	 * 获取全部有效的支付方式
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Area> getAll() throws Exception {
		List<Area> obs = null;
		try {
			obs = areaMapper.queryAll(new Area());
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return obs;
	}
}
